#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include<vector>
#include<fstream>
#include<String>

void txtTopcd(const char* inputfile, const char* outputfile)//设置输入输出参数，
{
	
	typedef struct tpXYZ//构建结构体
	{
		float x;
		float y;
		float z;
	};
	std::vector<tpXYZ> MyPCL;//声明存储vector变量
	tpXYZ points;//声明格式
        FILE *file;//声明函数
	file = fopen(inputfile, "r");//加载数据
	while (fscanf(file,"%f %f %f",&points.x,&points.y,&points.z)!=EOF)//读取并存储到vector中
	{
		MyPCL.push_back(points);
	}
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);//声明点云
	//设置点云参数
        int num = MyPCL.size();
	cloud->height = num;
	cloud->width = 1;
	cloud->is_dense = false;
	cloud->points.resize(cloud->height*cloud->width);
	for (size_t i = 0; i < num; i++)//点云赋值
	{
		cloud->points[i].x = MyPCL[i].x;
		cloud->points[i].y = MyPCL[i].y;
		cloud->points[i].z = MyPCL[i].z;
	}
	pcl::io::savePCDFileASCII(outputfile, *cloud);
	std::cerr << "转换成功！" << std::endl;
}
int main()
{
	const char* inp = "D:\\test.txt";//输入文件地址
	const char* otp = "D:\\test.pcd";//输出文件地址
	txtTopcd(inp, otp);
	return 0;
}
